/*
 *  hihocoder.com
 *
 *  Category: Dynamic Programming
 */

/*-------- Includes --------*/
#include <climits>
#include <iostream>
#include <algorithm>

using namespace std;

/*-------- Defines --------*/


/*-------- Types --------*/
typedef long long ll;

/*-------- Global Variables --------*/
int N;
int A[100002];
ll  d[100002];

/*-------- Function Prototypes --------*/


/*-------- Main Function --------*/
int main(int argc, char const *argv[])
{
    cin >> N;

    for (int i = 1; i <= N; ++i) cin >> A[i];

    sort(A + 1, A + N + 1);

    d[0] = 0;

    ll sum = 0;
    for (int i = 1; i <= N; ++i) {
        ll &ans = d[i];

        ans  = LLONG_MIN;
        sum += A[i];

        for (int j = i; j > 0 && i - j < 3; --j) {
            ans = max(ans, sum - d[j - 1]);
        }
    }

    cout << d[N] << endl;

    return 0;
}

/*-------- Functions --------*/

